<?php

function _views_xml_2attribute($field) {
  return $field['label'] .'="'. $field['value'] .'"';
}

function template_preprocess_views_view_fields_tag(&$vars) {
  $view = $vars['view'];
  $options = $vars['options'];
  $row_plugin = $view->style_plugin->row_plugin;
  $row = $vars['row'];

  $handlers = $row_plugin->display->handler->get_handlers('field');

  $attributes = array();
  $tags = array();

  foreach ($handlers as $key => $field_handler) {
    $field = array(
      'label' => $options['fields_options'][$key]['tag_name'],
      'value' => $field_handler->theme($row),
      'attributes' => array(),
    );

    if (!empty($options['root_value'])) {
      if ($key == $options['root_value']) {
        $root_value = $field['value'];
      }
      else {
        $attributes[] = _views_xml_2attribute($field);
      }
    }
    else {
      if (! empty($options['fields_options'][$key]['parent'])) {
        $parent = $options['fields_options'][$key]['parent'];
        $tags[$parent]['attributes'][] = _views_xml_2attribute($field); 
      }
      elseif ($options['fields_options'][$key]['is_attribute']) {
          $attributes[] = _views_xml_2attribute($field);
      }
      elseif (is_array($tags[$key])){
        $tags[$key] += $field;
      }
      else {
        $tags[$key] = $field;
      }
    }

  }

  $vars['tags'] = $tags;
  $vars['attributes'] = $attributes;
  $vars['root_value'] = $root_value;
}

function template_preprocess_views_view_xml(&$vars) {
  $view = $vars['view'];
  $vars['root'] = $view->style_plugin->options['root_tag'];
  
  //exit(print_r(($view->display[$view->current_display]->handler->handlers['field']['field_name_value']), 1));
  
  //exit(print_r($view->field, 1));
  //exit(print_r($view->style_plugin->options, 1));
}
